<template>
	<view class="my-settle-container">
		<label class="radio">
			<radio color="#c00000" :checked="isFullCheck" @click="radioHandler" /><text>全选</text>
		</label>
		<view class="amount-box">
			合计:<text class="amount">￥{{totalPrice}}</text>
		</view>
		<view class="btn-settle" @click="settlement">
			结算({{total}})
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-settle",
		computed: {
			...mapGetters('m_cart', ['total', 'totalPrice', 'allTotal']),
			...mapGetters('m_user', ['addstr']),
			...mapState({
				cart: state => state.m_cart.cart
			}),
			...mapState({
				token: state => state.m_user.token
			}),
			isFullCheck() {
				return this.total === this.allTotal
			}
		},
		data() {
			return {
				seconds: 3,
				timer: null
			};
		},
		methods: {
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			radioHandler() {
				this.updateAllGoodsState(!this.isFullCheck)
			},
			settlement() {
				if (!this.total) {
					return uni.showToast({
						icon: 'error',
						title: "你鸡毛没选",
					})
				}
				if (!this.addstr) {
					return uni.showToast({
						icon: 'error',
						title: "往哪儿送啊?",
					})
				}
				if (!this.token) {
					// return uni.showToast({
					// 	icon: 'error',
					// 	title: "请先登录",
					// })
					return this.delayNavigate()
				}
				this.payOrder()
			},
			//倒计时
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '请先登录!' + n + '秒后跳转到登录页',
					mask: true,
					duration: 1500
				})
			},
			delayNavigate() {
				this.showTips(this.seconds);
				this.timer = setInterval(() => {
					this.seconds--
					if (this.seconds === 0) {
						clearInterval(this.timer);
						this.seconds = 3;
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
						return
					}
					this.showTips(this.seconds)
				}, 1000)

			},
			//支付功能开发
			payOrder() {
				const orderInfo = {
					order_price: 0.01, //这里应该是总价格 store里的totalPrice
					consignee_adder: this.addstr, //收货地址
					gooods: this.cart.filter(v => v.goods_state).map(v => ({
						goods_id: v.goods_id,
						goods_number: v.goods_count,
						goods_price: v.goods_price
					}))
				}
				//获取订单编号
				let orderNumber = null
				uni.request({
					url: 'https://api-hmugo-web.itheima.net/api/public/v1/my/orders/create',
					method: "POST",
					data: orderInfo,
					header: {
						Authorization: this.token
					},
					success: (res) => {
						// console.log(res, 'res')
						if (res.data.meta.status !== 200) {
							return uni.showToast({
								icon: 'error',
								title: "创建订单失败"
							})
						}
						orderNumber = res.data.message.order_number
						//获取订单的支付信息 预支付
						uni.request({
							url: 'https://api-hmugo-web.itheima.net/api/public/v1/my/orders/req_unifiedorder',
							method: 'POST',
							data: {
								order_number: orderNumber
							},
							header: {
								Authorization: this.token
							},
							success: (res) => {
								// console.log(res,'获取订单的支付信息')
								if (res.data.meta.status !== 200) {
									return uni.showToast({
										icon: 'error',
										title: "获取订单支付信息失败"
									})
								}
								const payInfo = res.data.message.pay
								uni.requestPayment({
									provider:'alipay',
									orderInfo: payInfo,
									success: (res) => {
										console.log(res, '支付成功')
									},
									fail: (err) => {
										console.log(err, '支付失败')
										uni.showToast({
											icon:'error',
											title:'哎，支付失败',
											mask:true
										})
									}
								})
							},
							fail: (err) => {
								console.log(err, 'err')
							}
						})
					},
					fail: (err) => {
						console.log(err, 'err')
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 28rpx;
		padding-left: 20rpx;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount-box {
			.amount {
				font-weight: bold;
				color: #c00000;
			}
		}

		.btn-settle {
			height: 100%;
			color: #fff;
			display: flex;
			align-items: center;
			background-color: #c00000;
			padding: 0 30px;
		}
	}
</style>
